package com.beiye.drivertransport.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;


/**
 * Created by ky on 2018/1/5.
 */

  public class CustomView extends View
{
    private Paint paint;
    private Paint paint2;
    private ArrayList<Integer> letterList = new ArrayList<>();
    private int letterH;
    private int index;
    private boolean isTouch;
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
        initPaint();
        initData();
        invalidate();
    }

    public CustomView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        count=20;
        initPaint();
        initData();

    }



    private void initData()
    {
           letterList.clear();
            count=getCount();
            for (int i = 1; i <=count; i++)
            {
                letterList.add(i);
            }
    }

    private void initPaint()
    {
        paint = new Paint();
        paint.setStrokeWidth(2);
        paint.setTextSize(35);
        paint.setTextAlign(Paint.Align.CENTER);

        paint2 = new Paint();
        paint2.setStrokeWidth(4);
        paint2.setTextSize(50);
        paint2.setTextAlign(Paint.Align.CENTER);
        paint2.setColor(Color.RED);
        paint2.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        int w = canvas.getWidth();
        int h = canvas.getHeight();
        letterH = h/letterList.size();
        canvas.translate(w/2, 0);
        for (int i = 0; i < letterList.size(); i++)
        {
            if (isTouch && index ==i )
            {
                canvas.drawText(letterList.get(i)+"", 0, letterH*(i+1), paint2);
            } else
            {
                canvas.drawText(letterList.get(i)+"", 0, letterH*(i+1), paint);
            }
        }

    }
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();
        float y = event.getY();
        index = (int) (y/letterH);
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                if (lister!=null)
                {

                    if(letterList.size()==index)
                    {

                        lister.onTouch(letterList.get(index-1));
                    }else{
                        lister.onTouch(letterList.get(index));
                    }
                }
                isTouch = true;
                break;
            case MotionEvent.ACTION_MOVE:
                if (lister!=null)
                {

                    if(letterList.size()==index)
                    {

                        lister.onTouch(letterList.get(index-1));
                    }else{
                        if(index>0)
                        {

                            lister.onTouch(letterList.get(index));
                        }
                    }
                }
                isTouch = true;
                break;
            case MotionEvent.ACTION_UP:
                if (lister!=null)
                {
                    lister.up();
                }
                isTouch = false;
                break;

            default:
                break;
        }
        invalidate();
        return true;
    }
    public interface OnTouchLister
    {
        void onTouch(int s);
        void up();
    }
    private OnTouchLister lister;
    public void setOnTouchMyLister(OnTouchLister l)
    {
        lister = l;
    }
}

